package com.lzx.demo.bloodpressurehelper.fragment.home;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.lzx.demo.bloodpressurehelper.MainActivity;
import com.lzx.demo.bloodpressurehelper.R;
import com.lzx.demo.bloodpressurehelper.bean.CalendarBean;
import com.lzx.demo.bloodpressurehelper.fragment.home.adapter.CalendarAdapter;
import com.lzx.demo.bloodpressurehelper.fragment.home.adapter.CalendarWeekAdapter;
import com.lzx.demo.bloodpressurehelper.myView.MyCalendarView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/10/25.
 */
public class CalendarFragment extends Fragment {

    private List<CalendarBean> mCalendarBeans;
    private GridView calendarView;
    private GridView calendarWeekView;
    private SQLiteDatabase db;
    private MainActivity mainActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_calendar, container, false);
        MyCalendarView myCalendarView = new MyCalendarView();
        mainActivity = (MainActivity) getActivity();
        init();
        String[][] dateDays = myCalendarView.getDateDays();
        mCalendarBeans = new ArrayList<CalendarBean>();
        for (int row=0;row<dateDays.length;row++){
            String[] data =dateDays[row];
            for (int column = 0;column<data.length;column++){
                Integer day =data[column]==null?null: Integer.valueOf(data[column]);
                mCalendarBeans.add(new CalendarBean(myCalendarView.getYear(),
                        myCalendarView.getMonth(),
                        day,
                        column,
                        null));
            }
        }

        calendarView=(GridView)view.findViewById(R.id.calendar_view);
        CalendarAdapter calendarAdapter = new CalendarAdapter(mCalendarBeans, getContext(),db);
        calendarView.setAdapter(calendarAdapter);
        calendarWeekView = (GridView)view.findViewById(R.id.calendar_week_view);
        calendarWeekView.setAdapter(new CalendarWeekAdapter(getContext()));
        return  view;
    }

    private void init(){
        db=mainActivity.getmDBOpenHelper().getWritableDatabase();
    }

}
